package ua.pp.pycb6a.seposition.helper;

import static ua.pp.pycb6a.seposition.helper.WriterHelper.getWriter;
import static ua.pp.pycb6a.seposition.helper.LoggerHelper.getLogger;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.http.client.fluent.Request;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public final class PageContentHelper {

    private static final int TIMEOUT = 5000;
    private static String pageContent;

    private PageContentHelper() {

    }

    public static String getPageContent(String uri) {

        try {
            pageContent = Request.Get(uri)
                    .addHeader("User-Agent", "seposition/0.0.3")
                    .connectTimeout(TIMEOUT).socketTimeout(TIMEOUT).execute()
                    .returnContent().toString();
        } catch (IOException e) {

            getWriter().print(" CONNECTION ERROR");
        }

        return pageContent;
    }

    private static Document getDocument() {

        Document document = null;

        TagNode tagNode = new HtmlCleaner().clean(pageContent);
        try {
            document = new DomSerializer(new CleanerProperties())
                    .createDOM(tagNode);
        } catch (ParserConfigurationException e) {
            getWriter().print(" PAGE PARSING ERROR");
        }

        return document;

    }

    public static Node getNode(String expression) {

        Node node = null;
        try {

            XPath xPath = XPathFactory.newInstance().newXPath();

            node = (Node) xPath.compile(expression).evaluate(getDocument(),
                    XPathConstants.NODE);

        } catch (XPathExpressionException e) {

            getWriter().print(" PAGE PARSING ERROR");
        }

        return node;
    }

    public static NodeList getNodeList(String expression) {

        NodeList nodelist = null;
        try {

            XPath xPath = XPathFactory.newInstance().newXPath();

            nodelist = (NodeList) xPath.compile(expression).evaluate(
                    getDocument(), XPathConstants.NODESET);

        } catch (XPathExpressionException e) {

            getWriter().print(" PAGE PARSING ERROR");
        }

        return nodelist;
    }

    public static String encodeUri(String uri) {

        String enUri = null;
        try {
            enUri = URIUtil.encodeQuery(uri);
        } catch (URIException e) {

            getWriter().print(" ENCODE URI ERROR");
        }
        getLogger().info("Go to " + enUri);
        return enUri;
    }

    public static String substringBetween(String str, String open, String close) {

        if (str == null || open == null || close == null) {
            return null;
        }
        int start = str.indexOf(open);
        if (start != -1) {
            int end = str.indexOf(close, start + open.length());
            if (end != -1) {
                return str.substring(start + open.length(), end);
            }
        }
        return null;
    }

}
